package com.yq.netty.nio;

import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @ClassName NIOFileChannel01
 * @Description 本地文件写
 * @Author yaoqiang
 * @Date 2021/3/24 9:58
 * @Version 1.0
 */
public class NIOFileChannel01 {
    public static void main(String[] args) throws IOException {
        String str="hello";
        //创建输出流
        FileOutputStream fileOutputStream = new FileOutputStream("D://a.txt");
        //通过输出流获取对应的channel
        FileChannel channel = fileOutputStream.getChannel();
        //创建缓冲区
        ByteBuffer allocate = ByteBuffer.allocate(1024);
        //将str放入缓冲区
        ByteBuffer byteBuffer = allocate.put(str.getBytes());
        //反转
        allocate.flip();
        //将缓冲区的写入channel
        channel.write(byteBuffer);
        fileOutputStream.close();

    }
}
